//
//  QJNearVCHeaderSearchJumpVC.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/23.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFSearchShopViewController.h"
//数据为空
#import "UIScrollView+EmptyDataSet.h"
#import "YFSearchBar.h"
#import "QJNearVCTCell.h"

#import "QJNearVCShopDetailVC.h"

@interface YFSearchShopViewController () <UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property (nonatomic, strong) UITableView *searchTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) NSString *searchStr;
@property (nonatomic, strong) YFSearchBar *searchBar;

@end

@implementation YFSearchShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     //基本布局
    [self setupNav];
    [self setupSearchUIAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //数据请求
    [self nearHeaderSearchVCDataRequestAction];
}

#pragma mark -- 界面布局
- (void)setupNav {
    
    // 搜索框
    CGFloat searchBarH = 44 * 0.7;
    CGFloat searchBarY = (44 - searchBarH)/2;
    YFSearchBar *searchBar = [[YFSearchBar alloc] initWithFrame:CGRectMake(0, searchBarY, 0, searchBarH)]; // 后面会设置titleView,所以设置宽度没用
    searchBar.backgroundColor = [UIColor whiteColor];
    searchBar.font = [UIFont systemFontOfSize:14];
    //    searchBar.textColor = [UIColor colorWithHexString:@"999999"];
    searchBar.placeholder = @"搜索店铺";
    searchBar.layer.borderWidth = 0.5;// 边框
    searchBar.layer.borderColor = [UIColor colorWithHexString:@"CCCCCC"].CGColor;
    searchBar.layer.cornerRadius = searchBarH/2; // 这里先写死
    self.navigationItem.titleView = searchBar;
    self.searchBar = searchBar;
    // 放大镜
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    searchBar.leftView = imgView;
    //    searchBar.delegate = self;
    searchBar.leftViewMode = UITextFieldViewModeAlways;
    // 搜索按钮
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStyleDone target:self action:@selector(searchClick)];
    rightBtn.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBtn;
}
- (void)setupSearchUIAction{
    self.pageNum = 1;
    self.searchStr = @"";
    
    CGFloat top = IOS_VERSION < 11.0 ? Height_NavBar : 0;
    // 无故往下面偏移了
    self.searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, top-32, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:self.searchTableView];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    self.searchTableView.emptyDataSetDelegate = self;
    self.searchTableView.emptyDataSetSource = self;
    self.searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.searchTableView registerNib:[UINib nibWithNibName:@"QJNearVCTCell" bundle:nil] forCellReuseIdentifier:@"cellID"];
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (IOS_VERSION < 11.0) {
        //解决表无故偏移
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    // 刷新
    self.searchTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HRefresh)];
    self.searchTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(FRefresh)];
}
#pragma mark -- 按钮点击方法
- (void)searchClick {
    self.searchStr = self.searchBar.text;
    self.pageNum = 1;
    [self nearHeaderSearchVCDataRequestAction];
}

#pragma mark -- 刷新加载方法
- (void)HRefresh{
    self.pageNum = 1;
    [self nearHeaderSearchVCDataRequestAction];
}
- (void)FRefresh{
    self.pageNum++;
    [self nearHeaderSearchVCDataRequestAction];
}
#pragma mark -- 数据请求方法
- (void)nearHeaderSearchVCDataRequestAction{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.searchStr forKey:@"keywords"];
    [dict setObject:[NSString stringWithFormat:@""] forKey:@"categoryNearId"];
    [dict setObject:@"0" forKey:@"order"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)self.pageNum] forKey:@"page"];
    [[HttpManager shareHttpManager] postDictWithUrl:@"api/index.php/home/index/getNearShopList" parameter:dict hiddenHUD:YES HUDTitle:@"数据加载中..." success:^(NSURLSessionDataTask *operetion, id response) {
        if (self.pageNum == 1) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *data = response[@"data"];
        [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NearShopModel *model = [[NearShopModel alloc]initWithDictionary:obj error:nil];
            [self.dataSourceArray addObject:model];
        }];
        [self.searchTableView reloadData];
    } failure:^(NSURLSessionDataTask *operetion, id response) {
    }];
    [self.searchTableView.mj_header endRefreshing];
    [self.searchTableView.mj_footer endRefreshing];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"Cell";
    QJNearVCTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QJNearVCTCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cellDistanceLabel.hidden = YES;
    NearShopModel *model = self.dataSourceArray[indexPath.row];
    [cell cellUpdateWithModel:model];
    return cell;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QJNearVCShopDetailVC *VC = [[QJNearVCShopDetailVC alloc]init];
    NearShopModel *model = self.dataSourceArray[indexPath.row];
    VC.shopID = model.id;
    KPushViewControllerAction(VC);
}
#pragma mark -- DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"noDataImage"];
}
#pragma mark -- DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
#pragma mark -- 懒加载
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
